$(document).ready(function() {
	Messenger.options = {
			extraClasses: 'messenger-fixed messenger-theme-flat messenger-on-top messenger-on-right'
	}
    $('#login').bootstrapValidator({
        message: '数据填写不符合要求',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            username: {
                validators: {
                    notEmpty: {
                        message: '用户名不能为空'
                    },
                    stringLength: {
                        min: 5,
                        max: 50,
                        message: '用户名输入不足5位'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '密码输入不足6位'
                    }
                }
            },
            captcha: {
                validators: {
                    notEmpty: {
                        message: '验证码不能为空'
                    },
                    stringLength: {
                        min: 5,
                        max: 5,
                        message: '验证码位数不正确'
                    }
                }
            }
        }
    }).on('success.form.bv', function(e) {
        // 防止表单提交
        e.preventDefault();

        // 获取表单实例
        var $form = $(e.target);

        // 得到bootstrapValidator实例
        var bv = $form.data('bootstrapValidator');

        // 使用ajax提交数据
        $.post($form.attr('action'), $form.serialize(), function(result) {
            if (result.status == 'OK') {
               /* $.gritter.add({
                    title: result.message,
                    class_name: 'gritter-success' + (!$('#submitBtn').get(0).checked ? ' submitBtn' : ''),
                    after_open: function() {
                        window.location.href = 'system/admin/portal';
                    }
                });*/
                Messenger().post({
                    message: result.message,
                    type: 'success',
                    hideAfter: 1,
                    hideOnNavigate: true,
                });
                	window.location.href = 'system/admin/portal';
            
            } else {
                Messenger().post({
                    message: result.message,
                    type: 'error',
                    hideAfter: 1,
                    hideOnNavigate: true
                });
                $('#captcha').val('');
                $('#imgCode').click();
            }
        }, 'json');
    });

    $('#imgCode').on('click', function() {
        var src = this.src;
        var url = changeCaptcha(src);
        this.src = url;
    });
});

$(document).keydown(function(e) {
    if (e.keyCode == 13) {
        $('#login').bootstrapValidator('validate');
    }
});

$("#submitBtn").on('click', function() {
    $('#login').bootstrapValidator('validate');
});

function changeCaptcha(url) {
    var timestamp = (new Date()).valueOf();
    var idx = url.indexOf("?");
    if (idx > 0) {
        url = url.substring(0, idx) + "?timestamp=" + timestamp;
    } else {
        url = url + "?timestamp=" + timestamp;
    }
    return url;
}
